import java.io.IOException;
import java.net.*;
import java.util.Scanner;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2023-04-05
 * Time: 9:30
 */
public class UdpEchoClient {
    private String serverIp;
    private int port;
DatagramSocket socket = null;
    public UdpEchoClient(String serverIp, int port) throws SocketException {
        this.serverIp = serverIp;
        this.port = port;
        socket = new DatagramSocket();
    }
    public void start() throws IOException {
        Scanner sc =new Scanner(System.in);
        while (true){
            System.out.println("请输入请求->");
            String request = sc.next();
            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),request.getBytes().length, InetAddress.getByName(serverIp),port);
            socket.send(requestPacket);
            DatagramPacket responsePacket = new DatagramPacket(new byte[5000] , 5000);
            socket.receive(responsePacket);
            String response = new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println("请求为"+request+"响应为"+response);
        }

    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient udpEchoClient = new UdpEchoClient("127.0.0.1",6666);
        udpEchoClient.start();
    }
}
